package com.lemon.smartsafety.ui.fragment.safe.data;

import android.os.Bundle;

import com.lemon.smartsafety.app.BundleKey;
import com.lemon.smartsafety.app.EventTag;
import com.lemon.smartsafety.app.RefreshType;
import com.lemon.smartsafety.entity.safe.company.CompanyInfo;
import com.lemon.smartsafety.http.service.SafeDataServices;
import com.lemon.smartsafety.listener.RecyclerTClickListener;
import com.lemon.smartsafety.ui.adapter.safe.data.SafeDataCompanyInfoAdapter;
import com.lemon.smartsafetynew.ui.fragment.base.BasePullToRefreshFragment;
import com.lemon.smartsafety.utils.app.UIHelper;
import com.lemon.smartsafety.utils.common.LoggerHelper;
import com.lemon.smartsafety.utils.common.StringUtils;

import java.util.List;

import kale.adapter.LemonRcvAdapter;

/**
 * Created by Cooliean on 16/3/24.
 */
public class CompanySearchResultFragment extends BasePullToRefreshFragment<CompanyInfo, List<CompanyInfo>> implements RecyclerTClickListener<CompanyInfo> {
    private             LoggerHelper loggerHelper = new LoggerHelper(CompanySearchResultFragment.class);
    public final static String       KEY_SEARCH   = "KEY_SEARCH";


    private String mSearchText = "";

    public static CompanySearchResultFragment newInstance() {
        CompanySearchResultFragment fragment = new CompanySearchResultFragment();
        return fragment;
    }

    @Override
    protected void initViews() {
        super.initViews();
        mSearchText = getArguments().getString(KEY_SEARCH);
    }

    @Override
    public boolean goRefreshOnCreate() {
        return true;
    }

    @Override
    public LemonRcvAdapter<CompanyInfo> getLemonAdapter() {
        if (mAdapter != null) return mAdapter;
        return new SafeDataCompanyInfoAdapter(mDataList, this);
    }

    @Override
    public String getHttpTag() {
        return EventTag.TAG_SAFE_DATA_COLLECTION_COMPANY_LIST;
    }

    @Override
    public void onPullRefresh() {
        SafeDataServices.init().getAllCompanyInfo(RefreshType.REFRESH, pageSize, currentPage, mSearchText);
    }


    @Override
    public void onLoadMore() {
        SafeDataServices.init().getAllCompanyInfo(RefreshType.LOAD_MORE, pageSize, currentPage, mSearchText);
    }

    @Override
    public void onItemClick(CompanyInfo data, int position) {
        if (StringUtils.nullValue(data.getCompany_code()).length() != 0) {
            SafeDataServices.init().setRecentCompany(data.getCompany_code());
        }
        Bundle bundle = new Bundle();
        bundle.putSerializable(BundleKey.COMPANY_INFO, data);
        UIHelper.showSafeDataCollectionMainPanel(getActivity(), bundle);
    }
}
